// cJSON_to_Object.cpp : 此文件包含 "main" 函数。程序执行将在此处开始并结束。
//

#include <iostream>
#include <string>
#include <map>
#include <vector>
#include "cJSON.h"

class JsonSerializer {
public:
	static std::string toJson(const cJSON* root) {
		if (root == nullptr) {
			return "";
		}

		char* jsonStr = cJSON_PrintUnformatted(root);
		std::string json(jsonStr);
		cJSON_free(jsonStr);
		return json;
	}

	static cJSON* fromJson(const std::string& jsonStr) {
		cJSON* root = cJSON_Parse(jsonStr.c_str());
		if (root == nullptr) {
			std::cerr << "Failed to parse JSON string: " << jsonStr << std::endl;
		}
		return root;
	}

	static void deleteJson(cJSON* root) {
		cJSON_Delete(root);
	}
};

class Example {
public:
	std::string string;
	std::map<std::string, std::string> map;
	std::vector<int> vector;
	int integer; // 新增int类型成员变量

	// 将Example对象转换为cJSON对象
	cJSON* toJson() const {
		cJSON* root = cJSON_CreateObject();

		cJSON_AddStringToObject(root, "string", string.c_str());

		cJSON* mapItem = cJSON_AddObjectToObject(root, "map");
		for (const auto& pair : map) {
			cJSON_AddStringToObject(mapItem, pair.first.c_str(), pair.second.c_str());
		}

		cJSON* vectorItem = cJSON_AddArrayToObject(root, "vector");
		for (const auto& item : vector) {
			cJSON_AddItemToArray(vectorItem, cJSON_CreateNumber(item));
		}

		cJSON_AddNumberToObject(root, "integer", integer); // 序列化int类型成员变量

		return root;
	}

	// 将cJSON对象还原为Example对象
	static Example fromJson(const cJSON* root) {
		Example example;

		if (root == nullptr) {
			return example;
		}

		// 解析string成员
		cJSON* stringItem = cJSON_GetObjectItem(root, "string");
		if (stringItem != nullptr && stringItem->type == cJSON_String) {
			example.string = stringItem->valuestring;
		}

		// 解析map成员
		cJSON* mapItem = cJSON_GetObjectItem(root, "map");
		if (mapItem != nullptr && mapItem->type == cJSON_Object) {
			cJSON* mapValue = mapItem->child;
			while (mapValue != nullptr) {
				if (mapValue->type == cJSON_String) {
					example.map[mapValue->string] = mapValue->valuestring;
				}
				mapValue = mapValue->next;
			}
		}

		// 解析vector成员
		cJSON* vectorItem = cJSON_GetObjectItem(root, "vector");
		if (vectorItem != nullptr && vectorItem->type == cJSON_Array) {
			cJSON* vectorValue = vectorItem->child;
			while (vectorValue != nullptr) {
				if (vectorValue->type == cJSON_Number) {
					example.vector.push_back(vectorValue->valueint);
				}
				vectorValue = vectorValue->next;
			}
		}

		// 解析integer成员
		cJSON* integerItem = cJSON_GetObjectItem(root, "integer");
		if (integerItem != nullptr && integerItem->type == cJSON_Number) {
			example.integer = integerItem->valueint;
		}
		return example;
	}
};

int main() {
	// 创建一个Example对象
	Example example;
	example.string = "Hello";
	example.map = { {"key1", "value1"}, {"key2", "value2"} };
	example.vector = { 1, 2, 3, 4, 5 };
	example.integer = 123; // 设置int类型成员变量
	// 将Example对象转换为cJSON对象
	cJSON* root = example.toJson();

	// 将cJSON对象转换为JSON字符串
	std::string jsonStr = JsonSerializer::toJson(root);
	std::cout << "Serialized JSON: " << jsonStr << std::endl;

	// 将JSON字符串转换为cJSON对象
	cJSON* parsedRoot = JsonSerializer::fromJson(jsonStr);

	// 将cJSON对象还原为Example对象
	Example parsedExample = Example::fromJson(parsedRoot);

	// 输出还原后的Example对象
	std::cout << "Parsed Example object:" << std::endl;
	std::cout << "string: " << parsedExample.string << std::endl;
	std::cout << "map: {";
	for (const auto& pair : parsedExample.map) {
		std::cout << pair.first << ": " << pair.second << ", ";
	}
	std::cout << "}" << std::endl;
	std::cout << "vector: {";
	for (const auto& item : parsedExample.vector) {
		std::cout << item << ", ";
	}
	std::cout << "}" << std::endl;
	std::cout << "integer: " << parsedExample.integer << std::endl;

	// 释放资源
	JsonSerializer::deleteJson(root);
	JsonSerializer::deleteJson(parsedRoot);

	return 0;
}